<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 *
 */

require_once ROOT_PATH . '/lib/models/leave/LeaveQuota.php';

class EXTRACTOR_LeaveQuota {

	private $parent_Leave;

	function __construct() {
		$this->parent_Leave = new LeaveQuota();
	}

	public function parseAddData($postArr) {

		$this->parent_Leave->setYear($postArr['year']);
		$this->parent_Leave->setEmployeeId($_SESSION['empID']);
		$this->parent_Leave->setLeaveTypeId($postArr['sltLeaveType']);
		$this->parent_Leave->setLeaveDate($postArr['txtLeaveDate']);
		$this->parent_Leave->setLeaveLength($postArr['sltLeaveLength']);
		$this->parent_Leave->setLeaveComments($postArr['txtComments']);

		return $this->parent_Leave;
	}


	/**
	 * Pares edit data in the UI form
	 *
	 * @param mixed $postArr
	 * @return Leave[]
	 */
	public function parseEditData($postArr) {            
		$objLeave = null;                
		for ($i=0; $i < count($postArr['txtLeaveTypeId']); $i++) {
			$tmpObj = LeaveQuota::getQuotaById($postArr['txtquotaId'][$i]);
                        if(is_null($tmpObj->getId())){
                           if(isset($postArr['txtEmployeeId'][$i])
                                    && isset($postArr['year']) 
                                    && isset($postArr['txtEmployeeId'][$i])){
                                        $tmpObj->setLeaveTypeId($postArr['txtLeaveTypeId'][$i]);
                                        $tmpObj->setYear($postArr['year']);
                                        $tmpObj->setEmployeeId($postArr['txtEmployeeId'][$i]);
                                    }else{
                                        continue;
                                    }
                        }                        
			$tmpObj->setNoOfDaysAllotted(isset($postArr['txtLeaveEntitled'][$i])?$postArr['txtLeaveEntitled'][$i]:0);
			$tmpObj->setLeaveTypeName($postArr['txtLeaveTypeName'][$i]);
			//$tmpObj->setNoOfDaysAllotted($postArr['no_of_days_allotted']);
			$objLeave[] = $tmpObj;
		}                
		return $objLeave;
	}
        
        
        public function parseAddExLeaveData($postArr) {            		           		
                $objLeave = new LeaveQuota();
                $objLeave->setYear($postArr['year']);
                $objLeave->setEmployeeId($postArr['txtEmployeeId']);
                $objLeave->setLeaveTypeId($postArr['txtLeaveTypeId']);
                $objLeave->setNoOfDaysAllotted($postArr['txtLeaveEntitled']);
                $objLeave->setLeaveTypeName($postArr['txtLeaveTypeName']);		
		return $objLeave;
	}
	/*
	 * Chinh Nguyen add
	 *  
	*/
	//get work time by  months
	
	function calWorktime($empID){
		$employee = new EmpInfo();
		$employeeJob = $employee->filterEmpJobInfo($empID);
		$joined_date1 = empty($employeeJob[0][5])?0:$employeeJob[0][5];
		$now = time();
		$now_str=mktime();
		list($year, $month, $day) = split("-", $joined_date1);
		$joined_date = mktime(0, 0, 0, $month, $day, $year);
		$months = $this->calcMonthDiff($joined_date,$now_str);
		
		return $months;
	}	
	
	//caculate number of months between 2 date	
	public function calcMonthDiff($ts_start, $ts_end){
 
	//error protection
	if($ts_start > $ts_end) return false;
	
	//debug
	$debug = true;
	
	$diff = 0;
	$arr_int_mths = array(1,2,3,4,5,6,7,8,9,10,11,12);
	$month_start = intval(date('m',$ts_start));
	
	if($debug) echo "month start = $month_start<br />";
	$year_start = intval(date('Y',$ts_start));
	if($debug) echo "year start = $year_start<br />";
	$month_end = intval(date('m',$ts_end));
	if($debug) echo "month end = $month_end<br />";
	$year_end = intval(date('Y',$ts_end));
	if($debug) echo "year end = $year_end<br />";

	//set loop limit to exceed max number of years difference
	$loop_limit = 10;
	$i = 0;
	
	while($i < $loop_limit ) {
	
		foreach($arr_int_mths as $k => $v) {
		
			if($year_start == $year_end && $month_start == $month_end) 
			break;
			if( $month_start >= $v) {
				$diff++;
				$month_start++;
			}
			
			if($v == 12) {
				$year_start++;
				$month_start = 1;
			}
			
			
			
		//end loop
		}
		
		$i++;
		
		if($year_start == $year_end && $month_start == $month_end) break;
	}
	
	return $diff;
	
	//EOF
	}
	
	
	
	//End Chinh Nguyen add
	/**
	 * Pares delete data in the UI form
	 *
	 * @param mixed $postArr
	 * @return Leave[]
	 */
	public function parseDeleteData($postArr) {
		$objLeave = null;

		for ($i=0; $i < count($postArr['cmbStatus']); $i++) {
			if ($postArr['cmbStatus'][$i] == 0) {
				$tmpObj = new Leave();
				$tmpObj->setLeaveId($postArr['id'][$i]);

				$objLeave[] = $tmpObj;
			}
		}

		return $objLeave;
	}

}
?>